<?php /*
	
*/ ?>
<?php include("system/config.inc.php");
$donotinclude = 1;
// IF admin is not logged in then it will redirect to the adminlogin page.
if(!isset($_SESSION['adminuserid']))
{
	header("location:index.php?msg=logfirst");
	die();
}

$msg= "";	

///// Insert opearstion of category
if(isset($_REQUEST['Submit']) && trim($_REQUEST['Submit'])=="Submit")
{
	$parent_id = $_REQUEST['parentId'];
	$varcategory=$_FILES['imagefile']['name'];
	$contid=0;
				$sql="SELECT max( `intsortorder` ) AS max
						FROM `tblprodcategory` ";
				$result=$obj_db->select($sql);	
				if($result)
				{
					$contid=$result[0]['max'];
					$contid++;
				}
		if(($_FILES['imagefile']['size'] > 2000000))
			{
				header("location:category.php?msg=imgszbg");
				die();
			} else {
			
	$sql="INSERT INTO `tblprodcategory` (`varcategoryimage` , `intparentid` , 
											`intsortorder`, `dtdateadded` , `dtlastmodified`) 
						VALUES ('$varcategory', '$parent_id', '$contid',NOW(), NOW() )";
													
	$insert=$obj_db->insert($sql);		
	$screen_id = mysql_insert_id($obj_db->CONN);
	if(isset($_REQUEST['uploading']) && trim($_REQUEST['uploading']) == "imageupload" && ($_FILES['imagefile']['name'])!="") {
				$targetpath = "../images/cat/";
				$ext=$_FILES['imagefile']['name'];
				$ext=explode(".",$ext);
				if($ext[1]=="jpg" || $ext[1]=="gif" || $ext[1]=="jpeg" || $ext[1]=="png" || $ext[1]=="bmp" || $ext[1]=="wbmp" || $ext[1]=="JPEG" || $ext[1]=="JPG")
				{
				if($_FILES['imagefile']['size'] <= 2000000)
				{
				$filename=$targetpath.$ext[0].$screen_id.".".$ext[1];
				$fl_db=$ext[0].$screen_id.".jpg";
					if(file_exists($filename)) {
						chmod($filename, 0777);
						unlink($filename);
					}
					if(move_uploaded_file($_FILES['imagefile']['tmp_name'],$filename))
					{
						$targetpath = "../images/cat";
						$file=createThumb($filename , $targetpath , $ext[0].$screen_id , $_SESSION['config_val'][0]['intcategoryimage']);
						$sql= "UPDATE tblprodcategory SET varcategoryimage = '".$fl_db."' WHERE intcategoryid = '".$screen_id."' ";
						$res=$obj_db->sql_query($sql);
					}
				}
				else
					{
						header("location:category.php?msg=imgszbg");
						die();
					}
				}
			}
			
			$catid=$screen_id;
			$sql="select * from tbllanguages";
			$res=$obj_db->select($sql);
			if(count($res)>0) {
				for($l=0;$l<count($res);$l++) {
				$lid=$res[$l]['intlangaugeid'];
				$liid="language_".$lid;
					if(isset($_REQUEST[$liid]) && ($_REQUEST[$liid])!="") {
						$catname = mysql_real_escape_string($_REQUEST[$liid]);
						$sql1="INSERT INTO `tblprodcategorydesc` (`intcatid` , `intlanguageid` , 
															`varcategoryname`) 
										VALUES ('$catid', '$lid', '$catname')";
						$insert=$obj_db->insert($sql1);						
					}
				}
			}
		}
	header("location:category.php?msg=add");
	die();
}
/////////////////// END  of INSERT ///////////////////////////////////



//// UPDATE the record
$Parentid = "";
$p_catimg="";
//$Category="";
//$Description="";
$action=1;
if((isset($_REQUEST['action']) && trim($_REQUEST['action'])==2) && 
(!(isset($_REQUEST['Submit']) && trim($_REQUEST['Submit']) == "Submit")))
{
 	if(isset($_REQUEST['catid']) && trim($_REQUEST['catid'])!="")
 	{
		$screen_id = $_REQUEST['catid'];
		$sql = "select * from tblprodcategory where intcategoryid ='$screen_id'";
		$result = $obj_db->select($sql);
			
		if($result)
		{
			foreach($result as $row)
			{
				$Parentid = $row['intparentid'];
				$p_catimg=$row['varcategoryimage'];
				$action=2;
			}
		}
		
		if(isset($_REQUEST['Submit']) && trim($_REQUEST['Submit']) == "Update")
		{
			$parent_id = $_REQUEST['parentId'];
			$varcategory=$_FILES['imagefile']['name'];
			
		if(($_FILES['imagefile']['size'] > 2000000))
			{
				header("location:category.php?msg=imgszbg");
				die();
			}
			else
			{
			$sql = "UPDATE tblprodcategory 
			SET intparentid = '$parent_id'
			 WHERE intcategoryid = '$screen_id'";
			
			//$sql .= "intparentid = '$parent_id', ttdatecreated = NOW() WHERE intid = '$adsid'";
			$obj_db->sql_query($sql);
			$sql="select * from tbllanguages";
			$res=$obj_db->select($sql);
			if(count($res)>0)
			{
				for($l=0;$l<count($res);$l++)
				{
				$lid=$res[$l]['intlangaugeid'];
				$liid="language_".$lid;
					if(isset($_REQUEST[$liid]) && ($_REQUEST[$liid])!="")
					{
						//print($_REQUEST);
						$catname = mysql_real_escape_string($_REQUEST[$liid]);	
						$sql2="UPDATE tblprodcategorydesc SET varcategoryname = '$catname' WHERE intcatid = '$screen_id'";
						$obj_db->sql_query($sql2);
					}
				}
			}
			$action=1;
			// update images
			if(isset($_REQUEST['uploading']) && trim($_REQUEST['uploading']) == "imageupload" && $_FILES['imagefile']['name']!="")
			{
				$targetpath = "../images/cat/";
				$ext=$_FILES['imagefile']['name'];
				$ext=explode(".",$ext);
				if($ext[1]=="jpg" || $ext[1]=="gif" || $ext[1]=="jpeg" || $ext[1]=="png" || $ext[1]=="bmp" || $ext[1]=="wbmp" || $ext[1]=="JPEG" || $ext[1]=="JPG")
				{
					if($_FILES['imagefile']['size'] <= 2000000)
					{
					$filename=$targetpath.$ext[0].$screen_id.".".$ext[1];
					$fl_db=$ext[0].$screen_id.".jpg";
					
						if(file_exists($filename))
						{
						chmod($filename, 0777);
						unlink($filename);
						}
						if(move_uploaded_file($_FILES['imagefile']['tmp_name'],$filename))
						{
							$targetpath = "../images/cat/";
							$file=createThumb($filename , $targetpath , $ext[0].$screen_id , $_SESSION['config_val'][0]['intcategoryimage']);
						$sql= "UPDATE tblprodcategory SET varcategoryimage = '".$fl_db."' WHERE intcategoryid = '".$screen_id."' ";
						$res=$obj_db->sql_query($sql);
						chmod($filename, 0777);
						unlink($filename);
						}
					
					}
					else
						{
							header("location:category.php?msg=imgszbg");
							die();
						}
					}
				}
			}
			header("location:category.php?msg=edit");
			die();
		}
	}
}
/////////////////// END  of EDIT ///////////////////////////////////



// DELETE record from database
if(isset($_REQUEST['action']) && trim($_REQUEST['action'])==3)
{
	if(isset($_REQUEST['catid']) && trim($_REQUEST['catid']!=""))
	{	
		$adsid =  $_REQUEST['catid'];
	
		$catid=Getcatiddel($adsid,0,0,$obj_db,0);
		
		$catid=explode(",",$catid);
		for($i=0;$i<count($catid);$i++)
		{
			$adsid=$catid[$i];
			$sql3 = "DELETE FROM `tblprodcategory` WHERE `intcategoryid` = '$adsid'";
			$delete=mysql_query($sql3);
			$sql4="delete from `tblprodcategorydesc` where `intcatid`='$adsid'";
			$delete1=mysql_query($sql4);
		}		
		header("location:category.php?msg=del");
		die();
	}
}
/////////////////// END  of DELETE ///////////////////////////////////




////////////// UP Sorting //////////////////////
if(isset($_REQUEST['action']) && trim($_REQUEST['action'])==6)
{
  $sortno=$_REQUEST['sort'];
  $sorttmp="";
  $sql="select * from tblprodcategory";
		   $sql_link=$obj_db->select($sql);
  
   			for($i=0;$i<count($sql_link);$i++)
   				{
					if($sql_link[$i]['intsortorder'] < $sortno )
					{
						$sorttmp= $sql_link[$i]['intsortorder'];
					}
				}
				
				$sql="UPDATE `tblprodcategory` SET 
				`intsortorder` = '".$sortno."',
				`dtdateadded` = dtdateadded
				WHERE `intsortorder` =".$sorttmp;
				
				 $result=$obj_db->edit($sql);	
				 
				$sql="UPDATE `tblprodcategory` SET 
				`intsortorder` = '".$sorttmp."',
				`dtdateadded` = dtdateadded
				 WHERE `intcategoryid` =".$_REQUEST['fid'];
				/*echo $sql;
				die();*/
			  $result=$obj_db->edit($sql);	
			header("Location:category.php");
			die();
				

}
if(isset($_REQUEST['action']) && trim($_REQUEST['action'])==7)
{
	$sortno=$_REQUEST['sort'];
  $sorttmp="";
  $sql="select * from tblprodcategory";
		   $sql_link=$obj_db->select($sql);
  			$cond=true;
   			for($i=0;$i<count($sql_link);$i++)
   				{
					if($sql_link[$i]['intsortorder'] > $sortno && $cond==true)
					{
						$sorttmp= $sql_link[$i]['intsortorder'];
						$cond=false;
					}
				}
				
				$sql="UPDATE `tblprodcategory` SET 
				`intsortorder` = '".$sortno."',
				`dtaddeddate` = dtaddeddate
				WHERE `intsortorder` =".$sorttmp;
				
				 $result=$obj_db->edit($sql);	
				 
				$sql="UPDATE `tblprodcategory` SET 
				`intsortorder` = '".$sorttmp."',
				`dtaddeddate` = dtaddeddate
				 WHERE `intcategoryid` =".$_REQUEST['fid'];

			  $result=$obj_db->edit($sql);	
			header("Location:category.php");
			die();
				

}

?>
<script language="javascript" src="js/category.js"></script>
<?php include("inc/header.php");?>
<body>

<table width="80%" border="0" align="center" cellpadding="0" cellspacing="2" class="middlebackground">
  <tr>
    <td colspan="3" align="center"><?php include(INC."top.inc.php"); ?>&nbsp;</td>
  </tr>
  <tr>
    <td width="2%">&nbsp;</td>
    <td width="96%"><?php
if(isset($_REQUEST['script']))
{
	if(trim($_REQUEST['script'])=='addcategory' || trim($_REQUEST['script'])=='editcategory')
	{
	?>
	<form action="" method="post" enctype="multipart/form-data" name="category" >
      <table width="75%" align="center" cellpadding="2" cellspacing="2" class="border">
      <tr class="top">
        <td align="center" class="bg1"><strong>
          <?php if($_REQUEST['script']=='addcategory'){echo "Add New Product Category...";}
	if($_REQUEST['script']=='editcategory'){echo "Edit Product Category...";}?>
        </strong></td>
      </tr>
      <tr >
        <td><table width="100%"  border="0" align="center" cellpadding="2" cellspacing="2">

            <tr>
              <td width="24%" align="right" class="sltimg">&nbsp;</td>
              <td colspan="2">&nbsp;</td>
            </tr>
            <tr>
              <td align="right" class="sltimg">&nbsp;</td>
              <td colspan="2" align="right">(* All fields are Required)</td>
            </tr>
            <tr>
              <td align="right" class="sltimg">Parent : </td>
			  <?php 
			  		
			  ?>
              <td colspan="2">
                <select name="parentId" id="parentId">
                  <option value="0">Root</option>
                  <?php 
					if($action == 2)
					{
						$selected = $Parentid;
					}
					else
					{
						$selected = 0;
					}	
					echo GetChild(0,0,$selected,$obj_db,1);
				?>
              </select></td>
            </tr>
            <tr>
              <td align="right" class="sltimg">*Category Image : </td>
              <td width="54%">
                <input name="imagefile" type="file" id="imagefile" />
            </td>
              <td width="22%"></td>
            </tr>
			<?php
            $sql="select * from tbllanguages";
			$res=$obj_db->select($sql);
			if(count($res)>0)
			{
				for($l=0;$l<count($res);$l++)
				{
					$lid=$res[$l]['intlangaugeid'];
					$liid="language_".$lid;
			?>
			<tr>
			
              <td align="right" class="sltimg">*Category(<?php echo $res[$l]['varcode'];?>) : </td>
			  
              <td colspan="2">
			  <?php	
			  if(isset($screen_id) && isset($screen_id)!="")
			  {		  
					$sql="select * from `tblprodcategorydesc` WHERE `intcatid`='$screen_id'";
					$resman = $obj_db->select($sql);
					if(count($resman)>0)
					{
			  
			  ?>
			  <input name="<?php echo $liid;?>" type="text" onBlur="if(this.value=='')	{alert('Please enter Name'); return false;}" value="<?php echo stripslashes($resman[0]['varcategoryname']);?>"/>
			  <?php
					}
				}
					else
					{
			 ?>
			 <input name="<?php echo $liid;?>" type="text" onBlur="if(this.value=='')	{alert('Please enter Name'); return false;}" />
			 <?php	
			 		}
				}
			}			
			 ?>			  </td>
		    </tr>
			
            <tr>
              <td>&nbsp;</td>
              <td colspan="2">&nbsp;</td>
            </tr>
            <tr>
              <td><input type="hidden" name="uploading" value="imageupload" /></td>
              <td colspan="2">&nbsp;</td>
            </tr>
            <tr>
              <td colspan="3" align="center"><input name="Submit" type="submit" class="btn" 
				 value="<?php echo ($action==2) ? "Update":"Submit"; ?>" onClick="return check();" />              </td>
            </tr>
        </table></td>
      </tr>
    </table>
</form>
      <?php
	}
}
else
{
?>
   <br />
   <br />
    <table width="80%" border="0" align="center" cellpadding="1" cellspacing="0" class="border">
      <tr>
        <td ><table width="100%" border="0" align="center" cellpadding="1" cellspacing="0">
            <tr align="center" class="line_top">
              <td width="100%" height="25" align="left" class="bg1"> <strong>Category...</strong></td>
              <td width="100%" align="right" class="bg1"><a href="category.php?script=addcategory" class="new">[NEW]</a> </td>
            </tr>
            <tr align="center">
			 <?php 
	   $rows="";
	 if(isset($_REQUEST['page']) && ($_REQUEST['page'])!="")
	 {
	 	$rows=$_REQUEST['page'];
	 
	  }	
	  ?><form name="form1" action="" method="post">
              <td colspan="2" align="left">
			  
			  Row per Page:
                <select name="page" onChange="document.form1.submit();">
                  <option value="0"<?php if($rows=='0'){echo "selected";}else{echo "";}?>>All</option>
                  <option value="5"<?php if($rows=='5'){echo "selected";}else{echo "";}?>>5 Per Page</option>
                  <option value="10"<?php if($rows=='10'){echo "selected";}else{echo "";}?>>10 Per Page</option>
                  <option value="15"<?php if($rows=='15'){echo "selected";}else{echo "";}?>>15 Per Page</option>
                </select>
				
				</td>
				</form>
              </tr>
        </table></td>
      </tr>
      <tr>
        <td class="box"><table width="100%"  border="0" align="center" cellpadding="2" cellspacing="2">
            <tr align="center" class="tblbg3">
              <td><strong><?php 
			if(isset($_REQUEST['sort']) && ($_REQUEST['sort'])=='alphaasc')
			{
			?>
			<a  class="ab1" href="category.php?sort=alphadesc">Name</a>
			<?php 
				}
				else if(isset($_REQUEST['sort']) && ($_REQUEST['sort'])=='alphadesc')
				{
			?><a class="ab1" href="category.php?sort=alphaasc">Name</a>
			<?php }
				else
				{
				 ?>
			 <a class="ab1" href="category.php?sort=alphadesc">Name</a>
			 <?php
		 }?></strong></td>
              <td><strong>Image</strong> </td>
			   <td><strong>Up Sorting</strong> </td>
			    <td><strong>Down Sorting</strong> </td>
              <td><strong>Edit</strong></td>
              <td><strong>Delete</strong></td>
            </tr>
            <?php
	
		/**************************************
				PAGING CODE START
		**************************************/
			if(isset($_REQUEST['page']) && ($_REQUEST['page'])!=0)
			{
				$rows=$_REQUEST['page'];
				$rowperpage=$rows;
			}else{$rowperpage=$rows=10000;}
			
			$tablename="tblprodcategory";
			$per_page_keywords= "";
			$per_page_sorts="";
			include("system/paging.inc.php");

		/**************************************
				PAGING CODE ENDING
		**************************************/

	if(isset($_REQUEST['sort']) && ($_REQUEST['sort'])=='alphaasc')
	{
	$sql_select = "select tblprodcategory.*,tblprodcategorydesc.varcategoryname from tblprodcategory,tblprodcategorydesc where tblprodcategory.intcategoryid=tblprodcategorydesc.intcatid order by `varcategoryname` asc Limit ".($page_no*$row_per_page).",".$row_per_page;
	}
	else if(isset($_REQUEST['sort']) && ($_REQUEST['sort'])=='alphadesc')
	{
	$sql_select = "select tblprodcategory.*,tblprodcategorydesc.varcategoryname from tblprodcategory,tblprodcategorydesc where tblprodcategory.intcategoryid=tblprodcategorydesc.intcatid order by `varcategoryname` desc Limit ".($page_no*$row_per_page).",".$row_per_page;
	}
	else
	{
	$sql_select = "select tblprodcategory.*,tblprodcategorydesc.varcategoryname from tblprodcategory,tblprodcategorydesc where tblprodcategory.intcategoryid=tblprodcategorydesc.intcatid ORDER BY `intsortorder` asc Limit ".($page_no*$row_per_page).",".$row_per_page;
	}
	$sql = $obj_db->select($sql_select);
	
	if($sql)
	{
	$i=0;
	for($j=0;$j<count($sql);$j++)
	 { 
		$pict_cat_id=$sql[$j]['intcategoryid'];
		 $i=$i+1;
		  ?>
            <tr class="<?php echo ($i%2==0)?"Hrnormal":"Hralter"; ?>" onMouseOver="this.className='Hrhover';" onMouseOut="this.className='<?php echo ($i%2==0)?"Hrnormal":"Hralter"; ?>';">
              <td align="center">
			  <?php echo $sql[$j]['varcategoryname']; ?>
			  </td>
              <td align="center"><img title="'<?php echo $sql[$j]['varcategoryname'];?>'" src="../images/cat/<?php echo $sql[$j]['varcategoryimage'];?>" align="img"/></td>
			  <td align="center"><a href="category.php?fid=<?php echo $pict_cat_id;?>&action=6&sort=<?php echo $sql[$j]['intsortorder'];?>" class="aa">Ascending</a></td>
				<td align="center"><a href="category.php?fid=<?php echo $pict_cat_id;?>&action=7&sort=<?php echo $sql[$j]['intsortorder'];?>" class="aa">Descending</a></td>
              <td align="center"><a Title="Click here to Edit" class="link" href="category.php?script=editcategory&amp;action=2&amp;catid=<?php echo $pict_cat_id;?>"><img src="images/edit.bmp" border="0"/></a></td>
              <td align="center"><a Title="Click here to Delete" class="link" href="category.php?action=3&amp;catid=<?php echo $pict_cat_id;?>" onClick="return confirm('Are you sure to delete this record ?');"><img src="images/delete.bmp" border="0"/></a></td>
            </tr>
            <?php 
		}
		?>
            <tr >
              <td colspan="6" align="center">
                <?php
pagindet_atbotttom_page($div_page_no,$page_no,$req_querystr,$total_db_rec,$row_per_page);
?>              </td>
            </tr>
            <?php
	}	
   ?>
        </table></td>
      </tr>
    </table>  
      <?php
}
?>
    
    </td>
    <td width="2%">&nbsp;</td>
  </tr>
  <tr>
    <td colspan="3" align="center"><?php include(INC."bottom.php"); ?></td>
  </tr>
</table>
</body>
</html>
